package com.wgtest.utils;

import com.wgtest.pojo.RequestParameter;
import com.wgtest.pojo.WeixinParam;
import org.bouncycastle.util.encoders.Base64Encoder;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

public class Paixu
{
    //将参数生成TReeMap
    public static TreeMap getTreeMap(RequestParameter p)

    {
        TreeMap<Object, Object> map = new TreeMap<>();
        map.put("app_id",p.getApp_id());
        map.put("method",p.getMethod());
        map.put("charset",p.getCharset());
        map.put("sign_type",p.getSign_type());
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        map.put("timestamp",format);
        map.put("version",p.getVersion());
        map.put("biz_content",p.getBiz_content());
        map.put("sign",p.getSign());
        map.put("notify_url",p.getNotify_url());
        map.put("return_url",p.getReturn_url());
        return map;

    }
    //生成待生成签名字符串
    public static  String getSifnStr(TreeMap map)
    {
        StringBuffer sb = new StringBuffer();
        map.forEach((key,value)->{
            if(key.toString().equals("sign"))
            {

            }
            else {
                sb.append("&");
                sb.append(key);
                sb.append("=");
                sb.append(value);
            }
        });
        return sb.toString();
    }
    //将value转编码
    public static String getparam(TreeMap map)
    {
        StringBuffer sb = new StringBuffer();
        try {
            map.forEach((key, value) -> {

                String encode = URLEncoder.encode(value.toString());
                sb.append("&");
                sb.append(key);
                sb.append("=");
                sb.append(encode);

            });
        }
        catch (Exception e)
        {
            return  null;
        }
        return  sb.toString();
    }
    //拼接字符串
    public static String pinjiefrom(TreeMap map)
    {
        StringBuffer sb = new StringBuffer();
        sb.append("<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body><form action=\"http://openapi.alipaydev.com/gateway.do \" method=\"post\">\n" +
                "<input type='hidden' name='app_id' value='2016102600761423'/>");

        return null;
    }
    public static String formPost(TreeMap map){
        String form =
                "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head><body>" +
                        "<form action=\"http://openapi.alipaydev.com/gateway.do \" method=\"post\">\n" +
                        "<input type='hidden' name='app_id' value='"+map.get("app_id")+"'/>\n" +
                        "<input type='hidden' name='biz_content' value='"+map.get("biz_content")+"'/>\n" +
                        "<input type='hidden' name='charset' value='"+map.get("charset")+"'/>\n" +
                        "<input type='hidden' name='method' value='"+map.get("method")+"'/>\n" +
                        "<input type='hidden' name='notify_url' value='"+map.get("notify_url")+"'/>\n" +
                        "<input type='hidden' name='return_url' value='"+map.get("return_url")+"'/>\n" +
                        "<input type='hidden' name='sign' value='"+map.get("sign")+"'/>\n" +
                        "<input type='hidden' name='sign_type' value='"+map.get("sign_type")+"'/>\n" +
                        "<input type='hidden' name='timestamp' value='"+map.get("timestamp")+"'/>\n" +
                        "<input type='hidden' name='version' value='"+map.get("version")+"'/>\n" +
                        "<input type='submit' value='支付'/>\n" +
                        "<script type='text/javascript'>document.forms[0].submit()</script></form></body></html>";

        return form;
    }

    public static TreeMap getTreeMap2(WeixinParam w)
    {
        TreeMap<Object, Object> map = new TreeMap<>();
        map.put("appid",w.getAppid());
        map.put("mch_id",w.getMch_id());
        map.put("nonce_str",w.getNonce_str());
        map.put("body",w.getBody());
        map.put("out_trade_no",w.getOut_trade_no());
        map.put("total_fee",w.getTotal_fee());
        map.put("notify_url",w.getNotify_url());
        map.put("trade_type",w.getTrade_type());
        map.put("scene_info",w.getScene_info());
        map.put("spbill_create_ip",w.getSpbill_create_ip());
        return map;

    }
    //map转成xml字符串
    public static String GetMapToXML(Map<String,String> param){
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        for (Map.Entry<String,String> entry : param.entrySet()) {
            sb.append("<"+ entry.getKey() +">");
            sb.append(entry.getValue());
            sb.append("</"+ entry.getKey() +">");
        }
        sb.append("</xml>");
        return sb.toString();
    }
    public static Map<String,String> readStringXmlOut(String xml) {
        Map<String,String> map = new HashMap<String,String>();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
            Element rootElt = doc.getRootElement();// 获取根节点
            List<Element> list = rootElt.elements();//获取根节点下所有节点
            for (Element element : list) {  //遍历节点
                map.put(element.getName(), element.getText()); //节点的name为map的key，text为map的value
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
